package com.tarena.lbs.marketing.web.service.impl;

import com.tarena.lbs.marketing.web.service.CacheService;
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
import org.redisson.api.RBucket;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;

@Service
public class CacheServiceImpl implements CacheService {

    @Resource
    private RedissonClient redissonClient;

    @Override
    public boolean lock(String key) {
        RLock rLock = redissonClient.getLock(key);
        return rLock.tryLock();
    }

    @Override
    public void setKey(String key,String value) {
        RBucket<String> bucket = redissonClient.getBucket(key);
        bucket.set(value,10, TimeUnit.SECONDS);
    }

    @Override
    public String getKey(String key) {
        RBucket<String> bucket = redissonClient.getBucket(key);
        return bucket.get();
    }
}
